import { IVerificationAdapter, Tools } from "../../common/index";
import { Order } from "../../domain/model/index";
import { BaseService } from "../../service/baseService";
import { VerificationException } from './verificationException';

export namespace DataVerification {
    class VerifyBase implements IVerificationAdapter {
        Verify(data) {
            data.terminal = {
                id: data.terminal.id,
                name: data.terminal.name
            }
            data.user = {
                id: data.user.id,
                name: data.user.name
            }
            return data;
        }
    }

    export class OpenTable extends VerifyBase implements IVerificationAdapter {
    }

    export class SetTableSettled extends VerifyBase implements IVerificationAdapter {
        Verify(data) {
            if (data.tableId == null || data.tableId == undefined) throw new VerificationException("tableId");
            data = super.Verify(data);
            data = Tools.DeleteHashKey(data);
            if (data.order == null || data.order == undefined) throw new VerificationException("Order");
            if (data.order.cart == null || data.order.cart == undefined) throw new VerificationException("Order.cart");
            if (data.order.cart.items == null || data.order.cart.items == undefined || data.order.cart.items.length == null || data.order.cart.items.length <= 0) {
                throw new VerificationException("Order.cart.items");
            }
            return data;
        }
    }

    export class CloseTable extends VerifyBase implements IVerificationAdapter {
    }
    export class FinishCleanTable extends VerifyBase implements IVerificationAdapter {
        Verify(data) {
            if (data.tableId == null || data.tableId == undefined) throw new VerificationException("tableId");
            return data;
        }
    }
    export class StartCleanTable extends VerifyBase implements IVerificationAdapter {
        Verify(data) {
            data = super.Verify(data);
            if (data.tablePartyIds == null || data.tablePartyIds == undefined) throw new VerificationException("tablePartyIds");
            if (data.tablePartyIds.length == null || data.tablePartyIds.length == undefined || data.tablePartyIds.length <= 0) throw new VerificationException("tablePartyIds");
            return data;
        }
    }

    export class MergeTable extends VerifyBase implements IVerificationAdapter {
    }

    export class CancelMergeTable extends VerifyBase implements IVerificationAdapter {
    }

    export class TransferTable extends VerifyBase implements IVerificationAdapter {
    }

    export class Served extends VerifyBase implements IVerificationAdapter {
    }

    export class LockTable implements IVerificationAdapter {
        Verify(data) {
            data.terminal = {
                id: data.terminal.id,
                name: data.terminal.name
            }
            return data;
        }
    }

    export class CancelTableSettled extends VerifyBase implements IVerificationAdapter {
    }

    export class CancelServed extends VerifyBase implements IVerificationAdapter {
    }

    export class OrderingDataVerify extends VerifyBase implements IVerificationAdapter {
        Verify(data) {
            let preOrder = data;
            if (!preOrder.extended) throw new VerificationException("extended");
            if (!preOrder.extended.tableParty) throw new VerificationException("extended.tableParty");

            data = Tools.DeleteHashKey(data);
            data = Tools.DeletePicture(data);
            return data;
        }
    }

    export class MergerOrderDataVerify extends VerifyBase implements IVerificationAdapter {
        Verify(data) {
            data = Tools.DeleteHashKey(data);
            data = Tools.DeletePicture(data);
            return data;
        }
    }



    export class PushCheckoutDataVerify extends VerifyBase implements IVerificationAdapter {
        Verify(data) {
            data = Tools.DeleteHashKey(data);
            data = Tools.DeletePicture(data);
            return data;
        }
    }

    export class augmentFrontStoreStockVerify extends VerifyBase implements IVerificationAdapter {
        Verify(data) {
            let frontStoreStock = data.frontStoreStock,
                producerRecord = data.producerRecord
            existValue(frontStoreStock.id, 'frontStoreStock.id', 'frontStoreStock.id 不能为空');
            existValue(frontStoreStock.quantity, 'frontStoreStock.quantity', 'frontStoreStock.quantity 不能为空');
            verifyNumber(frontStoreStock.quantity, 'frontStoreStock.quantity', '制作数量必须大于零');

            existValue(producerRecord.inventory, 'producerRecord.inventory', 'producerRecord.inventory 不能为空');
            existValue(producerRecord.inventory.id, 'producerRecord.inventory.id', 'producerRecord.inventory.id 不能为空');
            existValue(producerRecord.inventory.name, 'producerRecord.inventory.name', 'producerRecord.inventory.name 不能为空');

            existValue(producerRecord.terminal, 'producerRecord.terminal', 'producerRecord.terminal 不能为空');
            existValue(producerRecord.terminal.id, 'producerRecord.terminal.id', 'producerRecord.terminal.id 不能为空');
            existValue(producerRecord.terminal.name, 'producerRecord.terminal.name', 'producerRecord.terminal.name 不能为空');

            existValue(producerRecord.producer, 'producerRecord.producer', 'producerRecord.producer 不能为空');
            existValue(producerRecord.producer.id, 'producerRecord.producer.id', 'producerRecord.producer.id 不能为空');
            existValue(producerRecord.producer.name, 'producerRecord.producer.name', 'producerRecord.producer.name 不能为空');

            existValue(producerRecord.creator, 'producerRecord.creator', 'producerRecord.creator 不能为空');
            existValue(producerRecord.creator.id, 'producerRecord.creator.id', 'producerRecord.creator.id 不能为空');
            existValue(producerRecord.creator.name, 'producerRecord.creator.name', 'producerRecord.creator.name 不能为空');

            existValue(producerRecord.beginTime, 'producerRecord.beginTime', 'producerRecord.beginTime 不能为空');
            existValue(producerRecord.endTime, 'producerRecord.endTime', 'producerRecord.endTime 不能为空');

            return data;
        }
    }

    export class ScrapOutFrontStoreStocks extends VerifyBase implements IVerificationAdapter {
        Verify(data) {
            let creator = data.creator,
                terminal = data.terminal,
                frontStoreStocks = data.frontStoreStocks
            existValue(terminal, 'terminal', 'terminal 设备信息不能为空');
            existValue(terminal.id, 'terminal.id', 'terminal.id 不能为空');
            existValue(terminal.name, 'terminal.name', 'terminal.name 不能为空');

            if (!frontStoreStocks || (frontStoreStocks && frontStoreStocks.length == 0))
                throw new VerificationException("frontStoreStocks", "作废的商品不能为空！");
            for (let i = 0; i < frontStoreStocks.length; i++) {
                let stock = frontStoreStocks[i];
                existValue(stock.id, 'stock.id', 'frontStoreStock.id 不能为空');
                existValue(stock.quantity, 'stock.quantity', 'frontStoreStock.quantity 不能为空');
                verifyNumber(stock.quantity, 'stock.quantity', '报废数量必须大于零');
                existValue(stock.inventory, 'stock.inventory', 'frontStoreStock.inventory 不能为空');
                existValue(stock.inventory.id, 'stock.inventory.id', 'frontStoreStock.inventory.id 不能为空');
                existValue(stock.inventory.name, 'stock.inventory.name', 'frontStoreStock.inventory.name 不能为空');
                existValue(stock.inventory.code, 'stock.inventory.code', 'frontStoreStock.inventory.code 不能为空');
                existValue(stock.inventory.unit, 'stock.inventory.unit', 'frontStoreStock.inventory.unit 不能为空');
                existValue(stock.inventory.unit.name, 'stock.inventory.unit.name', 'frontStoreStock.inventory.unit.name 不能为空');
                existValue(stock.inventory.unit.id, 'stock.inventory.unit.id', 'frontStoreStock.inventory.unit.id 不能为空');
            }
            data = {
                creator,
                frontStoreStocks
            };
            return data;
        }
    }

    function existValue(value, objName, message) {
        if (!value)
            throw new VerificationException(objName, message);
    }

    function verifyNumber(value, objName, message) {
        if (value <= 0)
            throw new VerificationException(objName, message);
    }

    interface IVerifyData {
        user: { id: string; name: string };
        terminal: { id: string; name: string };
    }

    interface SetTableSettledData extends IVerifyData {
        order: Order;
        tableId: string;
    }
}